﻿using System;
using System.Data;
using System.Data.SqlClient;
using Eassets.Layered.Domain.Asset;
using System.Collections.ObjectModel;

namespace Eassets.Data.Asset
{
    public class AssetTransferOutDetailsData
    {
        private CommonData commonData;
        public AssetTransferOutDetailsData()
        {
            commonData = new CommonData();
        }

        /// <summary>
        /// Inserts assetTransferOutDetails.
        /// </summary>
        /// <param name="assetTransferOutDetails"></param>
        public void AddAssetTransferOutDetails(AssetTransferOutDetails assetTransferOutDetails)
        {
            SqlCommand command = new SqlCommand("SP_AddAssetTransferOutDetails"); // Name of the SP
            CommonData commonData = new CommonData();
            commonData.ExecuteNonQuery(ConstructCommand(command, assetTransferOutDetails));
        }

        /// <summary>
        /// Updates assetTransferOutDetails.
        /// </summary>
        /// <param name="assetTransferOutDetails"></param>
        public void ModifyAssetTransferOutDetails(AssetTransferOutDetails assetTransferOutDetails)
        {
            SqlCommand command = new SqlCommand("SP_ModifyAssetTransferOutDetails"); // Name of the SP
            command.Parameters.AddWithValue("@AssetTransferOutDetailsId", assetTransferOutDetails.AssetTransferOutId);
            CommonData commonData = new CommonData();
            commonData.ExecuteNonQuery(ConstructCommand(command, assetTransferOutDetails));
        }


        /// <summary>
        /// Delete assetTransferOutDetails.
        /// </summary>
        /// <param name="assetTransferOutDetailsId"></param>
        public void DeleteAssetTransferOutDetails(long assetTransferOutDetailsId)
        {
            SqlCommand command = new SqlCommand("SP_DeleteAssetTransferOutDetails"); // Name of the SP
            command.Parameters.AddWithValue("@AssetTransferOutDetailsId", assetTransferOutDetailsId);
            command.CommandType = CommandType.StoredProcedure;
            CommonData commonData = new CommonData();
            commonData.ExecuteNonQuery(command);
        }

        /// <summary>
        /// Get assetTransferOutDetails by id.
        /// </summary>
        /// <param name="assetTransferOutDetailsId"></param>
        /// <returns></returns>
        public AssetTransferOutDetails GetByIdAssetTransferOutDetails(long assetTransferOutDetailsId)
        {
            SqlCommand command = new SqlCommand("SP_GetAssetTransferOutDetailsById");
            command.Parameters.AddWithValue("@AssetTransferOutDetailsId", assetTransferOutDetailsId);
            command.CommandType = CommandType.StoredProcedure;
            CommonData commonData = new CommonData();
            SqlDataReader dataReader = commonData.ExecuteReader(command);
            AssetTransferOutDetails assetTransferOutDetails = new AssetTransferOutDetails();
            while (dataReader.Read())
            {
                if (!(String.IsNullOrEmpty(dataReader["AssetTransferOutId"].ToString())))
                {
                    assetTransferOutDetails.AssetTransferOutId = Convert.ToInt64(dataReader["AssetTransferOutId"]);
                }
                if (!(String.IsNullOrEmpty(dataReader["TransactionId"].ToString())))
                {
                    assetTransferOutDetails.TransactionId = Convert.ToInt64(dataReader["TransactionId"]);
                }
                if (!(String.IsNullOrEmpty(dataReader["CompanyId"].ToString())))
                {
                    assetTransferOutDetails.CompanyId = Convert.ToInt64(dataReader["CompanyId"]);
                }
                if (!(String.IsNullOrEmpty(dataReader["FromOrganizationLevelListId"].ToString())))
                {
                    assetTransferOutDetails.FromOrganizationLevelListId = Convert.ToString(dataReader["FromOrganizationLevelListId"]);
                }
                if (!(String.IsNullOrEmpty(dataReader["ToOrganizationLevelListId"].ToString())))
                {
                    assetTransferOutDetails.ToOrganizationLevelListId = Convert.ToString(dataReader["ToOrganizationLevelListId"]);
                }
                if (!(String.IsNullOrEmpty(dataReader["TransactionNumber"].ToString())))
                {
                    assetTransferOutDetails.TransactionNumber = Convert.ToString(dataReader["TransactionNumber"]);
                }
                if (!(String.IsNullOrEmpty(dataReader["AssetId"].ToString())))
                {
                    assetTransferOutDetails.AssetId = Convert.ToString(dataReader["AssetId"]);
                }
                if (!(String.IsNullOrEmpty(dataReader["ItemSerialCode"].ToString())))
                {
                    assetTransferOutDetails.ItemSerialCode = Convert.ToString(dataReader["ItemSerialCode"]);
                }
                if (!(String.IsNullOrEmpty(dataReader["TransactionQuantity"].ToString())))
                {
                    assetTransferOutDetails.TransactionQuantity = Convert.ToInt16(dataReader["TransactionQuantity"]);
                }
                if (!(String.IsNullOrEmpty(dataReader["UnitPrice"].ToString())))
                {
                    assetTransferOutDetails.UnitPrice = Convert.ToDecimal(dataReader["UnitPrice"]);
                }
                if (!(String.IsNullOrEmpty(dataReader["TotalPrice"].ToString())))
                {
                    assetTransferOutDetails.TotalPrice = Convert.ToDecimal(dataReader["TotalPrice"]);
                }
                if (!(String.IsNullOrEmpty(dataReader["OriginatedBy"].ToString())))
                {
                    assetTransferOutDetails.OriginatedBy = Convert.ToString(dataReader["OriginatedBy"]);
                }
                if (!(String.IsNullOrEmpty(dataReader["AssetStatus"].ToString())))
                {
                    assetTransferOutDetails.AssetStatus = Convert.ToString(dataReader["AssetStatus"]);
                }
                if (!(String.IsNullOrEmpty(dataReader["TransferDate"].ToString())))
                {
                    assetTransferOutDetails.TransferDate = Convert.ToDateTime(dataReader["TransferDate"]);
                }
            }
            dataReader.Close();
            return assetTransferOutDetails;
        }

        /// <summary>
        /// GetAll assetTransferOutDetails.
        /// </summary>
        /// <returns></returns>
        public Collection<AssetTransferOutDetails> GetAllAssetTransferOutDetails()
        {
            Collection<AssetTransferOutDetails> assetTransferOutDetailss = new Collection<AssetTransferOutDetails>();
            SqlCommand command = new SqlCommand("sproc_t_l_GetAllAssetTransferOutDetailss");
            command.CommandType = CommandType.StoredProcedure;
            CommonData commonData = new CommonData();
            SqlDataReader dataReader = commonData.ExecuteReader(command);
            AssetTransferOutDetails assetTransferOutDetails;
            while (dataReader.Read())
            {
                assetTransferOutDetails = new AssetTransferOutDetails();
                if (!(String.IsNullOrEmpty(dataReader["AssetTransferOutId"].ToString())))
                {
                    assetTransferOutDetails.AssetTransferOutId = Convert.ToInt64(dataReader["AssetTransferOutId"]);
                }
                if (!(String.IsNullOrEmpty(dataReader["TransactionId"].ToString())))
                {
                    assetTransferOutDetails.TransactionId = Convert.ToInt64(dataReader["TransactionId"]);
                }
                if (!(String.IsNullOrEmpty(dataReader["CompanyId"].ToString())))
                {
                    assetTransferOutDetails.CompanyId = Convert.ToInt64(dataReader["CompanyId"]);
                }
                if (!(String.IsNullOrEmpty(dataReader["FromOrganizationLevelListId"].ToString())))
                {
                    assetTransferOutDetails.FromOrganizationLevelListId = Convert.ToString(dataReader["FromOrganizationLevelListId"]);
                }
                if (!(String.IsNullOrEmpty(dataReader["ToOrganizationLevelListId"].ToString())))
                {
                    assetTransferOutDetails.ToOrganizationLevelListId = Convert.ToString(dataReader["ToOrganizationLevelListId"]);
                }
                if (!(String.IsNullOrEmpty(dataReader["TransactionNumber"].ToString())))
                {
                    assetTransferOutDetails.TransactionNumber = Convert.ToString(dataReader["TransactionNumber"]);
                }
                if (!(String.IsNullOrEmpty(dataReader["AssetId"].ToString())))
                {
                    assetTransferOutDetails.AssetId = Convert.ToString(dataReader["AssetId"]);
                }
                if (!(String.IsNullOrEmpty(dataReader["ItemSerialCode"].ToString())))
                {
                    assetTransferOutDetails.ItemSerialCode = Convert.ToString(dataReader["ItemSerialCode"]);
                }
                if (!(String.IsNullOrEmpty(dataReader["TransactionQuantity"].ToString())))
                {
                    assetTransferOutDetails.TransactionQuantity = Convert.ToInt16(dataReader["TransactionQuantity"]);
                }
                if (!(String.IsNullOrEmpty(dataReader["UnitPrice"].ToString())))
                {
                    assetTransferOutDetails.UnitPrice = Convert.ToDecimal(dataReader["UnitPrice"]);
                }
                if (!(String.IsNullOrEmpty(dataReader["TotalPrice"].ToString())))
                {
                    assetTransferOutDetails.TotalPrice = Convert.ToDecimal(dataReader["TotalPrice"]);
                }
                if (!(String.IsNullOrEmpty(dataReader["OriginatedBy"].ToString())))
                {
                    assetTransferOutDetails.OriginatedBy = Convert.ToString(dataReader["OriginatedBy"]);
                }
                if (!(String.IsNullOrEmpty(dataReader["AssetStatus"].ToString())))
                {
                    assetTransferOutDetails.AssetStatus = Convert.ToString(dataReader["AssetStatus"]);
                }
                if (!(String.IsNullOrEmpty(dataReader["TransferDate"].ToString())))
                {
                    assetTransferOutDetails.TransferDate = Convert.ToDateTime(dataReader["TransferDate"]);
                }

                assetTransferOutDetailss.Add(assetTransferOutDetails);
            }
            dataReader.Close();
            return assetTransferOutDetailss;
        }

        /// <summary>
        /// Get no. of items for an asset by asset id
        /// </summary>
        /// <param name="assetId"></param>
        /// <returns></returns>
        public long GetNoOfItemsForAnAsset(long assetId)
        {
            long noOfItems = 0;
            SqlCommand command = new SqlCommand("SP_GetNoOfItemsForAnAsset");
            command.Parameters.AddWithValue("@assetId", assetId);
            command.CommandType = CommandType.StoredProcedure;
            CommonData commonData = new CommonData();
            SqlDataReader dataReader = commonData.ExecuteReader(command);
            while (dataReader.Read())
            {
                if (!(String.IsNullOrEmpty(dataReader["noOfItems"].ToString())))
                {
                    noOfItems = Convert.ToInt64(dataReader["noOfItems"]);
                }
            }
            return noOfItems;
        }

        /// <summary>
        /// 
        /// </summary>
        /// <param name="assetId"></param>
        /// <returns></returns>
        public long GetNoOfAssetsInOrganizationLevelListAssetByAssetId(long assetId)
        {
            long noOfAssets = 0;
            SqlCommand command = new SqlCommand("SP_GetNoOfAssetsInOrganizationLevelListAssetByAssetId");
            command.Parameters.AddWithValue("@AssetId", assetId);
            command.CommandType = CommandType.StoredProcedure;
            CommonData commonData = new CommonData();
            SqlDataReader dataReader = commonData.ExecuteReader(command);
            while (dataReader.Read())
            {
                if (!(String.IsNullOrEmpty(dataReader["NoOfAssets"].ToString())))
                {
                    noOfAssets = Convert.ToInt64(dataReader["NoOfAssets"]);
                }
            }
            return noOfAssets;
        }

        public SqlCommand ConstructCommand(SqlCommand command, AssetTransferOutDetails assetTransferOutDetails)
        {
            command.CommandType = CommandType.StoredProcedure;

            try
            {
                command.Parameters.AddWithValue("@AssetTransferOutId", assetTransferOutDetails.AssetTransferOutId);
            }
            catch (Exception)
            { }
            try
            {
                command.Parameters.AddWithValue("@TransactionId", assetTransferOutDetails.TransactionId);
            }
            catch (Exception)
            { }
            try
            {
                command.Parameters.AddWithValue("@CompanyId", assetTransferOutDetails.CompanyId);
            }
            catch (Exception)
            { }
            try
            {
                command.Parameters.AddWithValue("@FromOrganizationLevelListId", assetTransferOutDetails.FromOrganizationLevelListId);
            }
            catch (Exception)
            { }
            try
            {
                command.Parameters.AddWithValue("@ToOrganizationLevelListId", assetTransferOutDetails.ToOrganizationLevelListId);
            }
            catch (Exception)
            { }
            try
            {
                command.Parameters.AddWithValue("@TransactionNumber", assetTransferOutDetails.TransactionNumber);
            }
            catch (Exception)
            { }
            try
            {
                command.Parameters.AddWithValue("@AssetId", assetTransferOutDetails.AssetId);
            }
            catch (Exception)
            { }
            try
            {
                command.Parameters.AddWithValue("@ItemSerialCode", assetTransferOutDetails.ItemSerialCode);
            }
            catch (Exception)
            { }
            try
            {
                command.Parameters.AddWithValue("@TransactionQuantity", assetTransferOutDetails.TransactionQuantity);
            }
            catch (Exception)
            { }
            try
            {
                command.Parameters.AddWithValue("@UnitPrice", assetTransferOutDetails.UnitPrice);
            }
            catch (Exception)
            { }
            try
            {
                command.Parameters.AddWithValue("@TotalPrice", assetTransferOutDetails.TotalPrice);
            }
            catch (Exception)
            { }
            try
            {
                command.Parameters.AddWithValue("@OriginatedBy", assetTransferOutDetails.OriginatedBy);
            }
            catch (Exception)
            { }
            try
            {
                command.Parameters.AddWithValue("@AssetStatus", assetTransferOutDetails.AssetStatus);
            }
            catch (Exception)
            { }
            try
            {
                command.Parameters.AddWithValue("@TransferDate", assetTransferOutDetails.TransferDate);
            }
            catch (Exception)
            { }
            return command;
        }

        //public Collection<String> GetTRODetailsByOrganizationLevelListId(long organizationLevelListId)
        //{
        //    SqlCommand command = new SqlCommand("SP_GetTRODetailsByOrganizationLevelListId");
        //    command.Parameters.AddWithValue("@OrgLevelListId", organizationLevelListId);
        //    command.CommandType = CommandType.StoredProcedure;
        //    SqlDataReader dataReader = CommonData.ExecuteReader(command);

        //    while (dataReader.Read())
        //    {

        //       if (!(String.IsNullOrEmpty(dataReader["EmployeeName"].ToString())))
        //        {
        //            employee.EmployeeName = Convert.ToString(dataReader["EmployeeName"]);
        //        }
        //        if (!(String.IsNullOrEmpty(dataReader["Email"].ToString())))
        //        {
        //            employee.Email = Convert.ToString(dataReader["Email"]);
        //        }
        //       }
        //    dataReader.Close();

        //}

    }
}
